<?php

class Faqs extends CWidget {

	public $modelId;

	public function run() {
		$model = new Ask('model');
		if (isset($_POST['Ask'])) {
			$model->attributes = $_POST['Ask'];
			$model->faqTitle = 'Вопрос от: ' . $model->faqUserName;
			$model->faqGlobalType = 'model';
			if ($model->save()) {
				Yii::app()->user->setFlash('success', ['Благодарим вас за обращение к нам.', 'Мы ответим вам как можно скорее.']);
				$this->getController()->refresh();
			}
		}

		$sql = 'SELECT
				q."faqId" AS "qId",
					q."faqTitle",
					q ."faqContent" as "question",
					A ."faqContent" as "answer",
					A ."faqMostUseful",
					q."faqCreateDate",
					q."faqUserName" as "qUser",
					a."faqUserName" as "aUser"
				FROM
					mb_faq q
				left join mb_faq A on A ."faqQid" = q."faqId"
				where q."faqModelId" = \'' . $this->modelId . '\' AND q."faqStatus" = 1 AND q."faqGlobalType" = \'model\' 
					order by  q."faqCreateDate" desc, a."faqCreateDate" desc';
		$faqs = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();


		$this->render('faqs', array('model' => $model, 'faqs' => $faqs));
	}

}
